package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"k8s-api/kubeclient"
	"net/http"
)

func GetServiceHandler(c *gin.Context) {
	skc := &kubeclient.ServiceKubeController{KubeController: kubeclient.K8sMap["k8s"]}
	namespace := c.Param("namespace")
	serviceName := c.Param("service")
	data, err := skc.Get(namespace, serviceName)
	if err != nil {
		fmt.Println("获取Service异常", err)
	}
	c.JSON(http.StatusOK, gin.H{
		"data": data,
	})
}

func CreateServiceHandler(c *gin.Context) {
	skc := &kubeclient.ServiceKubeController{KubeController: kubeclient.K8sMap["k8s"]}
	namespace := c.Param("namespace")
	applicationName := c.Param("service")
	data, err := skc.Create(namespace, applicationName)
	if err != nil {
		fmt.Println("创建Service异常", err)
	}
	c.JSON(http.StatusOK, gin.H{
		"data": data,
	})
}

func DeleteServiceHandler(c *gin.Context) {
	skc := &kubeclient.ServiceKubeController{KubeController: kubeclient.K8sMap["k8s"]}
	namespace := c.Param("namespace")
	applicationName := c.Param("service")
	err := skc.Delete(namespace, applicationName)
	if err != nil {
		fmt.Println("删除Service异常", err)
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "ok",
	})
}
